# src/schemas/base.py
from pydantic import BaseModel
from pydantic.alias_generators import to_camel


def to_camel_case(string: str) -> str:
    """snake_case → camelCase"""
    return to_camel(string)


class CamelModel(BaseModel):
    """
    - 自动支持小驼峰命名（camelCase）
    - 前端可用 camelCase 传入，后端可用 snake_case 访问
    - 输出 JSON 时自动转回 camelCase
    """
    model_config = {
        "alias_generator": to_camel_case,
        "populate_by_name": True,
        "from_attributes": True,
    }

    def model_dump(self, **kwargs):
        """FastAPI 响应 JSON 时自动 camelCase 输出"""
        kwargs.setdefault("by_alias", True)
        return super().model_dump(**kwargs)
